配置雙主模式
配置數(shù)據(jù)庫1
修改1服務(wù)器上的數(shù)據(jù)庫配置文件/etc/my.cnf,增加以下配置:
#設(shè)置server-id
server-id=1
#開啟二進制日志
log-bin=mysql-bin
#開啟中繼日志
relay-log=relay-log
#設(shè)置binlog日志格式
binlog_format=ROW
#設(shè)置不進行同步的數(shù)據(jù)庫
binlog-ignore-db=mysql
binlog-ignore-db=sys
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#設(shè)置并行復(fù)制線程數(shù),推薦和cpu核數(shù)保持一致
slave_parallel_workers=8
#設(shè)置組提交的并行復(fù)制方式
slave_parallel_type=logical_clock
#開啟gtid模式
gtid_mode=on
#設(shè)置GTID的一致性
enforce_gtid_consistency=ON
#開啟中繼日志恢復(fù)
relay-log-recovery=1
#設(shè)置中繼日志信息記錄的方式
relay-log-info-repository=TABLE
#設(shè)置同步master.info事務(wù)間隔
sync_master_info=1
#設(shè)置master.info記錄方式
master_info_repository=TABLE
#設(shè)置binlog日志過期清理時間
expire_logs_days=30
#設(shè)置登錄驗證方式
default_authentication_plugin=mysql_native_password
#開啟不區(qū)分大小寫
#在配置文件內(nèi)使用該參數(shù)需要清空mysql的數(shù)據(jù)目錄,rm -rf /var/lib/mysql/*
#而且在安裝mysql后需要執(zhí)行 ?mysqld --initialize --lower-case-table-name=1
lower_case_table_names = 1
default-time_zone='+08:00'
登錄mysql創(chuàng)建同步用戶
mysql> CREATE USER 'repl'@'192.168.1.101' IDENTIFIED BY 'Welcome1!';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.101';
mysql> flush privileges;?
重啟mysql
systemctl restart mysqld
查看mysql的狀態(tài)
show master status
配置數(shù)據(jù)庫2
修改1服務(wù)器上的數(shù)據(jù)庫配置文件/etc/my.cnf,增加以下配置:
#設(shè)置server-id,不能和mysql重復(fù)
server-id=2
#開啟二進制日志
log-bin=mysql-bin
#開啟中繼日志
relay-log=relay-log
#設(shè)置binlog日志格式
binlog_format=ROW
#設(shè)置不進行同步的數(shù)據(jù)庫
binlog-ignore-db=mysql
binlog-ignore-db=sys
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#設(shè)置并行復(fù)制線程數(shù),推薦和cpu核數(shù)保持一致
slave_parallel_workers=8
#設(shè)置組提交的并行復(fù)制方式
slave_parallel_type=logical_clock
#開啟gtid模式
gtid_mode=on
#設(shè)置GTID的一致性
enforce_gtid_consistency=ON
#開啟中繼日志恢復(fù)
relay-log-recovery=1
#設(shè)置中繼日志信息記錄的方式
relay-log-info-repository=TABLE
#設(shè)置同步master.info事務(wù)間隔
sync_master_info=1
#設(shè)置master.info記錄方式
master_info_repository=TABLE
#設(shè)置binlog日志過期清理時間
expire_logs_days=30
#設(shè)置登錄驗證方式
default_authentication_plugin=mysql_native_password
#開啟不區(qū)分大小寫
#在配置文件內(nèi)使用該參數(shù)需要清空mysql的數(shù)據(jù)目錄,rm -rf /var/lib/mysql/*
#而且在安裝mysql后需要執(zhí)行 ?mysqld --initialize --lower-case-table-name=1
lower_case_table_names = 1
default-time_zone='+08:00'
登錄mysql創(chuàng)建同步用戶
mysql> CREATE USER 'repl'@'192.168.1.100' IDENTIFIED BY 'Welcome1!';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.100';
#設(shè)置該賬號使用普通賬號認證
mysql> alter user'repl'@'10.120.72.24' identified with mysql_native_password by 'Xdzn@2022';
mysql> flush privileges;?
重啟mysql
systemctl restart mysqld
查看mysql的狀態(tài)
show master status
查看狀態(tài)時,只有兩臺顯示的一致結(jié)果一致時才會同步成功
配置同步
進入服務(wù)器2,登錄mysql,執(zhí)行同步語句
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.100',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='Welcome1!',master_auto_position=1;
啟動同步
mysql> start slave
mysql> show slave status\G;
#Slave_IO_Running和Slave_SQL_Running都顯示為YES說明同步成功
?